<?php
namespace app\admin\controller;

use think\Session;
use app\common\controller\AdminBase;
use app\common\model\Title as TitleModel;
use app\common\model\Imgcategory as CategoryModel;


class Title extends AdminBase
{
    protected $category_model;
    protected $title_model;

    public function _initialize(){
        parent::_initialize();
        $this->category_model = new CategoryModel();
        $this->title_model = new TitleModel();
        $category_level_list  = $this->category_model->getLevelList();
        $this->assign('imgcategory_level_list', $category_level_list);
    }

    public function title($page = 1 , $cid = 0 , $keyword = ''){
        $where = [
            'status' => ['eq',1],
        ];
        if(!empty($keyword)){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        $title_list = $this->title_model->field('id,title,text,examine,path,create_time')->where($where)->paginate(15,false,['page'=>$page,'query' => ['keyword' => $keyword, 'cid' => $cid]]);
        $this->assign('title_list' , $title_list);
        return $this->fetch('title',[
            'keyword'=>$keyword,
            'page'=>$page,
            'cid'=>$cid,
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function save(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $validate_result = $this->validate($data,'title');
            if(!$validate_result){
                return $this->error($validate_result);
            }else{
                $data['admin_id'] = session::get('admin_id');
                $pattern = "/&lt;img src=&quot;([\s\S]+?)&quot;/";
                preg_match($pattern,$data['text'],$matches);
                if(!empty($matches)){
                    $data['path'] = $matches[1];
                }
                $result = $this->title_model->allowField(true)->save($data);
                if($result){
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }
        }
    }

    public function edit($id){
        $where = [
            'examine' => ['eq',1],
            'status' => ['eq',1],
            'id' => ['eq',$id],
        ];
        $result = $this->title_model->where($where)->find();
        $this->assign('title_info',$result);
        return $this->fetch();
    }

    public function update($id){
        if($this->request->isPost()){
            $data = $this->request->post();
            $validate_result = $this->validate($data,'title');
            if(!$validate_result){
                return $this->error($validate_result);
            }else{
                $data['admin_id'] = session::get('admin_id');
                $pattern = "/&lt;img src=&quot;([\s\S]+?)&quot;/";
                preg_match($pattern,$data['text'],$matches);
                if(!empty($matches)){
                    $data['path'] = $matches[1];
                }
                $result = $this->title_model->allowField(true)->save($data,['id' => $id]);
                if($result){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }
        }
    }

    public function delete($id){
        $where = [
            'id' => ['eq',$id],
        ];
        $result = $this->title_model->save(['status' => 0],$where);
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 审核
     */
    public function toggle(){
        $data = $this->request->param();
        if($data['type'] == 'audit'){
            //审核通过
            $ids = implode(',',$data['ids']);
            $result = $this->title_model->where('id','in',$ids)->update(['examine'=>1]);
            if($result){
                $this->success('审核已通过');
            }else{
                $this->error('审核失败');
            }
        }elseif($data['type'] == 'cancel_audit'){
            //审核不通过
            $ids = implode(',',$data['ids']);
            $result = $this->title_model->where('id','in',$ids)->update(['examine'=>2]);
            if($result){
                $this->success('审核已拒绝');
            }else{
                $this->error('审核失败');
            }
        }
    }
}